package com.blade.vo;

/**
 * 统一Ajax请求返回VO
 */
public class AjaxResponseVO {
    // 编码：指示错误代码
    private int code;

    // 错误信息：对应于code
    private String msg;

    // 返回结果
    private Object result;

    /**
     * 给定错误编码、返回消息、返回数据
     * @param code 错误代码
     * @param msg 返回消息
     * @param result 返回结果
     * @return AjaxResponseVO对象
     */
    public static AjaxResponseVO of(int code, String msg, String result) {
        AjaxResponseVO vo = new AjaxResponseVO();
        vo.code = code;
        vo.msg =msg;
        vo.result =result;

        return vo;
    }

    /**
     * 仅返回响应代码和消息
     * @param responseCode 响应编码
     * @return AjaxResponseVO对象
     */
    public static AjaxResponseVO of(ResponseCode responseCode) {
        AjaxResponseVO vo = new AjaxResponseVO();
        vo.code = responseCode.code();
        vo.msg = responseCode.message();

        return vo;
    }

    /**
     * 给定响应的数据，并返回默认的成功响应代码和成功消息
     * @param result 返回结果
     * @return AjaxResponseVO对象
     */
    public static AjaxResponseVO of(Object result) {
        AjaxResponseVO vo = new AjaxResponseVO();
        vo.result = result;
        vo.code = ResponseCode.SUCCESS.code();
        vo.msg = ResponseCode.SUCCESS.message();

        return vo;
    }
    /**
     * 给定响应的数据和响应的消息值，并返回默认的处理成功的响应代码
     * @param result 返回结果
     * @param msg 返回消息
     * @return AjaxResponseVO对象
     */
    public static AjaxResponseVO of(Object result, String msg) {
        AjaxResponseVO vo = new AjaxResponseVO();
        vo.result = result;
        vo.code = ResponseCode.SUCCESS.code();
        vo.msg = msg;

        return vo;
    }

    /**
     * 给定响应代码和消息
     * @param responseCode 响应编码、消息对象
     * @return AjaxResponseVO对象
     */
    public AjaxResponseVO code(ResponseCode responseCode) {
        this.code = responseCode.code();
        this.msg = responseCode.message();

        return this;
    }

    /**
     * 给定响应的数据
     * @param result 返回结果
     * @return AjaxResponseVO对象
     */
    public AjaxResponseVO result(Object result) {
        this.result = result;

        return this;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }
}
